//
//  HBUserInfoController.m
//  HeartBeat
//
//  Created by karong on 2023/9/5.
//

#import "HBUserInfoController.h"
#import "GRDatePickerView.h"

@interface HBUserInfoController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, GRDatePickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *userImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *userNameBtn;
@property (weak, nonatomic) IBOutlet UIButton *userIdBtn;
@property (weak, nonatomic) IBOutlet UIButton *userSexBtn;
@property (weak, nonatomic) IBOutlet UIButton *userBirthdayBtn;

@property (nonatomic, strong) UIImagePickerController * imagePickerController;

@end

@implementation HBUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self layoutView];
}

- (void)layoutView {
    [self.userImageBtn sd_setImageWithURL:[NSURL URLWithString:self.user.avatar] forState:(UIControlStateNormal) placeholderImage:HBUserPlaceholderImage];
    [self.userNameBtn setTitle:self.user.user_name forState:(UIControlStateNormal)];
    [self.userIdBtn setTitle:SF(@"%ld", (long)self.user.id) forState:(UIControlStateNormal)];
    [self.userSexBtn setTitle:(@[WSLocalized(@"未知"), WSLocalized(@"男"), WSLocalized(@"女"), WSLocalized(@"未知")])[self.user.sex] forState:(UIControlStateNormal)];
    [self.userBirthdayBtn setTitle:self.user.birthday forState:(UIControlStateNormal)];
}

- (void)editUserWithField:(NSString *)field stringValue:(NSString *)value success:(空回调)success {
    [HBToast loading];
    [HBRequest editUserInfoWithField:field stringValue:value success:^{
        [HBToast dismiss];
        success();
        [self layoutView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)editUserWithField:(NSString *)field integerValue:(NSInteger)value success:(空回调)success {
    [HBToast loading];
    [HBRequest editUserInfoWithField:field integerValue:value success:^{
        [HBToast dismiss];
        success();
        [self layoutView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (IBAction)userImageAction:(id)sender {
    [ViewManager showUserAvatarWithSelectHandle:^(NSInteger integer) {
        if (integer == 1) {
            //相冊
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
        }
        else if (integer == 2) {
            //拍照
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
        }
    } inController:self];
}

- (IBAction)userNameAction:(id)sender {
    WeakSelf
    [ViewManager showTextFieldAlterWithTitle:WSLocalized(@"修改暱稱") content:self.user.user_name placeholderStr:WSLocalized(@"修改暱稱")maxWordCount:10 confirm:@"" confirmHandle:^(NSString *string) {
        [weakSelf editUserWithField:@"user_name" stringValue:string success:^{
            weakSelf.user.user_name = string;
        }];
    } cancel:@"" inController:self];
}

- (IBAction)userSexAction:(id)sender {
    WeakSelf
    [ViewManager showUserSexWithSelectHandle:^(NSInteger integer) {
        //1男 2女
        [weakSelf editUserWithField:@"sex" integerValue:integer success:^{
            weakSelf.user.sex = integer;
        }];
    } inController:self];
}

- (IBAction)userBirthdayAction:(id)sender {
    GRDatePickerView* datePickerView = [[GRDatePickerView alloc] init];
    datePickerView.delegate = self;
    [datePickerView setDefaultSelectDay:self.user.birthday dateFormat:@"yyy-MM-dd"];
    [datePickerView show];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [self.userImageBtn setImage:image forState:(UIControlStateNormal)];
        // 提交頭像
        WeakSelf
        [HBRequest uploadImage:image success:^(NSString *string) {
            [weakSelf editUserWithField:@"avatar" stringValue:string success:^{
                weakSelf.user.avatar = string;
            }];
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

        }];
    }];
    
}

- (UIImagePickerController *)imagePickerController {
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        _imagePickerController.allowsEditing = YES;
    }
    return _imagePickerController;
}

#pragma mark GRDatePickerViewDelegate
- (void)pickerDateView:(BasePickerView *)pickerDateView selectYear:(NSInteger)year selectMonth:(NSInteger)month selectDay:(NSInteger)day {
    NSString * string = SF(@"%ld-%ld-%ld", (long)year, (long)month, (long)day);
    [self editUserWithField:@"birthday" stringValue:string success:^{
        self.user.birthday = string;
    }];
}

@end
